<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Order extends Model
{
    protected $guarded = ['id'];
    protected $appends = ['thumb','total_price_txt','total_amount_txt','product_num','user_status','status_info','status_info_tip','dispatch_info']; 

    private $statusTxt = [
        //售后完成
         '-100' => ['user'=>'已退款','shop'=>'已退款','style'=>'warning'], 
         '-50' => ['user'=>'拼团失败','shop'=>'拼团失败','style'=>'warning'],
        
        '-3' => ['user'=>'已过期','shop'=>'已过期', 'style'=>'danger'],

        '-2' =>['user'=>'已取消','shop'=>'已取消（超时未支付）', 'style'=>'danger'],
        '-1' => ['user'=>'已取消','shop'=>'已取消','style'=>'warning'],
    	'1' => ['user'=>'待付款','shop'=>'待付款','style'=>'primary'],
    	//线上付款才有用
        '2' => ['user'=>'待发货','shop'=>'待发货','style'=>'success'], 
    	'3' => ['user'=>'待发货','shop'=>'已确认','style'=>'purple'],
        '4' => ['user'=>'待收货','shop'=>'已发货','style'=>'purple'], 
        '5' => ['user'=>'待收货','shop'=>'已发货','style'=>'purple'], 
        

        
        '50' => ['user'=>'待成团','shop'=>'待成团','style'=>'info'],
        '55' => ['user'=>'拼团成功，等待发货','shop'=>'已成团','style'=>'info'], //实物
        '60' => ['user'=>'拼团成功，等待核销','shop'=>'已成团','style'=>'info'], //消费券

        

    	'100' => ['user'=>'已完成','shop'=>'已完成','style'=>'success'],
    	'200' => ['user'=>'已评价','shop'=>'已评价','style'=>'success'],
    ];
    
    public function address()
    {
        return $this->belongsTo(UserAddress::class);
    }

    public function userAddress()
    {
        return $this->belongsTo(UserAddress::class,'address_id');
    }

    public function user()
    {
    	return $this->belongsTo(User::class);
    }

    public function products()
    {
    	return $this->hasMany(OrderProduct::class);
    }

    public function shop()
    {
        return $this->belongsTo(Shop::class);
    }

    public function getUserStatusAttribute()
    {    
        // 消费券
        if($this->product_type==2 && ($this->status==4 ||$this->status==60) ) {
            return '待核销';
        }

       
    	return $this->statusTxt[$this->status]['user'];
    }

    public function getShopStatusAttribute()
    {    	
        if($this->product_type==2 && ($this->status==4 ||$this->status==60) ) {
            return '待核销';
        }
    	return $this->statusTxt[$this->status]['shop'];
    }

    public function getStatusStyleAttribute()
    {       
        return $this->statusTxt[$this->status]['style'];
    }

    public function getTotalPriceTxtAttribute()
    {       
        return $this->total_price/100;
    }

    public function getTotalAmountTxtAttribute()
    {       
        return $this->total_amount/100;
    }

    public function getUserMoneyTxtAttribute()
    {
        return $this->user_money/100;
    }

    public function getStatusArrAttribute()
    {
        return $this->statusTxt;
    }

    public function getProductNumAttribute()
    {
        return $this->products->count();
    }

    public function getStatusInfoAttribute()
    {
        $info['-50']= '订单取消';
        $info['-3'] = '订单已过有效期';
        $info['-1'] = '订单取消';
        $info['-2'] = '您已取消订单，交易已关闭';
        $info['1']  = '订单待支付，快去付款啦';
        $info['2']  = '支付成功，等待商户发货';
        $info['4']  = '商户已发货，等待确认收货';
       
        $info['50'] = '支付成功，等待成团';
        $info['55'] = '拼团成功，等待商户发货';
        $info['60'] = '拼团成功，券码已发放';
        $info['100'] = '订单已完成';
        if($this->product_type==2) {
            $info['4']  = '支付成功，券码已发放';       
        }

        
        return array_get($info,$this->status);
    }

    public function getStatusInfoTipAttribute()
    {
        if($this->dispatched_at) {
            $auto_received_at = Carbon::parse($this->dispatched_at)->addHours(2);
            $info['4']  = $auto_received_at.' 后将自动确定收货';
        }
        if($this->product_type==2) {
            $op = OrderProduct::where(['order_id'=>$this->id])
                                ->first();
            $validity_end = object_get($op,'product.validity_end');
            $info['60'] = $info['4']  = '有效期：'.$validity_end.' 到期（过期作废）';
        }
        $info['1']  = Carbon::parse($this->created_at)->addHours(2).' 自动取消';
        
       
        if($this->status==50) {
            $ogWhere['order_id']  = $this->id;
            $ogWhere['parent_id'] = 0;
            $og = OrderGroup::where(['group_code'=>$this->group_code])
                            ->first();
            $info['50'] = object_get($og,'end_at').' 后未成团自动取消订单';    
        }


        

        return array_get($info,$this->status);
    }

    public function getDispatchInfoAttribute()
    {
        $info['-50']= '您的订单由于拼团失败，交易已关闭，退款将原路返回';
        //核销订单
        $info['-3'] = '您的订单已超过使用有效期，无法使用';
        $info['-1'] = '您的订单由于超时支付，交易已关闭';
        $info['1']  = '订单提交成功后，请在2小时内付款，若未及时付款，系统将自动取消订单';
        $info['2']  = '等待商户发货 <p>商户名称：'.object_get($this,'shop.name').' </p><p>商户电话：'.object_get($this,'shop.phone').'</p>';
        $info['4']  = '快递公司：'.$this->dispatch_company.' 快递单号：'.$this->dispatch_code;
        
        $info['50'] = '支付成功，等待成团，限定时间内未成团自动取消订单';
        $info['55'] = '拼团成功，等待商家发货 <p>商户名称：'.object_get($this,'shop.name').' </p><p>商户电话：'.object_get($this,'shop.phone').'</p><p> 商户地址：'.object_get($this,'shop.address').'</p>';

        if($this->product_type==2) {
        
            $info['4'] = $info['60']  = '<p>商户名称：'.object_get($this,'shop.name').'</p> <p>商户电话：'.object_get($this,'shop.phone').' </p><p>商户地址：'.object_get($this,'shop.address').'</p>';
        }
        $info['100'] = '订单已完成 <p>商户名称：'.object_get($this,'shop.name').' </p><p>商户电话：'.object_get($this,'shop.phone').'</p><p> 商户地址：'.object_get($this,'shop.address').'</p>';
        
        return array_get($info,$this->status);
    }

    public function getThumbAttribute()
    {
        $op = OrderProduct::where(['order_id'=>$this->id])->first();
        return object_get($op,'product.thumb');
    }
}
